package com.briup.product_source.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.briup.product_source.dao.ManagerFenceHouseMapper;
import com.briup.product_source.dao.ext.ManagerFenceHouseExtMapper;
import com.briup.product_source.exception.ServiceException;
import com.briup.product_source.pojo.ManagerFenceHouse;
import com.briup.product_source.pojo.ext.ManagerFenceHouseExt;
import com.briup.product_source.result.ResultCode;
import com.briup.product_source.service.FenceHouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class FenceHouseServiceImpl implements FenceHouseService {
    @Autowired
    private ManagerFenceHouseMapper managerFenceHouseMapper;
    @Autowired
    private ManagerFenceHouseExtMapper managerFenceHouseExtMapper;
    @Override
    public PageInfo<ManagerFenceHouse> findByPage(Integer pageNum, Integer pageSize, String fhName) {
    //1.PageHelper开启分页
        PageHelper.startPage(pageNum, pageSize);
    //2.核心查询语句
        List<ManagerFenceHouse> list = managerFenceHouseMapper.queryAllHouses(fhName);
    //3.将查询的信息封装在PageInfo对象中，返回
        PageInfo<ManagerFenceHouse> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<ManagerFenceHouse> getAll() {
        // 调用之前分页且条件查询，条件传空即可
        return managerFenceHouseMapper.queryAllHouses(null);
    }

    @Override
    public ManagerFenceHouseExt getDetailByFhId(String fhId) throws ServiceException {
        return managerFenceHouseExtMapper.selectByFhId(fhId);
    }

    @Override
    public void saveOrUpdate(ManagerFenceHouse managerFenceHouse) throws ServiceException {
        String fhId = managerFenceHouse.getFhId();
        String fhName = managerFenceHouse.getFhName();
        // fhName不能为空
        if(StrUtil.isBlank(fhName)){
            throw new ServiceException(ResultCode.FENCE_HOUSE_NAME_IS_BLANK);
        }
    List<ManagerFenceHouse> list = managerFenceHouseMapper.queryAllHouses(fhName);
    // 判断新增还是修改：根据参数中是否有fhId
        if(StrUtil.isBlank(fhId)){
        // fhName不能重复
        if(CollUtil.isNotEmpty(list)){
            throw new ServiceException(ResultCode.FENCE_HOUSE_IS_EXIST);
        }
        // 新增
        // 因为没有fhId，所以需要生成一个uuid
        managerFenceHouse.setFhId(IdUtil.fastSimpleUUID());
        managerFenceHouseMapper.insert(managerFenceHouse);
    }else {
        // 更新： fhName和之前不一样才更新，和之前一样就不更新名字
        ManagerFenceHouse existingFenceHouse = list.stream()
                .filter(s -> fhId.equals(s.getFhId()))
                .findFirst()
                .orElse(null);

        if (existingFenceHouse != null && fhName.equals(existingFenceHouse.getFhName())) {
            managerFenceHouse.setFhName(null);
            managerFenceHouseMapper.updateSelective(managerFenceHouse);
        } else {
            managerFenceHouseMapper.update(managerFenceHouse);
            }
        }
    }
    //根据id删除单个栏舍
    @Override
    public void deleteById(String id) throws ServiceException {
        // 查询一下要删除的栏舍下面有没有栏圈
        ManagerFenceHouseExt ext = managerFenceHouseExtMapper.selectByFhId(id);
        if(Objects.isNull(ext)){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        if(CollUtil.isNotEmpty(ext.getMhs())){
            throw new ServiceException(ResultCode.DATA_CAN_NOT_DELETE);
        }
        managerFenceHouseMapper.deleteByFhId(id);
    }
    //批量删除
    public void removeByIdAll(List<String> ids) throws ServiceException {
        // 参数校验
        if(CollUtil.isEmpty(ids)){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        // 判断有效id
        // 准备一个集合来储存有效id
        List<String> validIds = CollUtil.newArrayList();
        ids.forEach(id->{
            ManagerFenceHouseExt ext = managerFenceHouseExtMapper.selectByFhId(id);
            // 当ext不为空的时候，继续判断其中的mhs是否有效
            if (ext != null){
                if(CollUtil.isEmpty(ext.getMhs())){
                    validIds.add(ext.getFhId());
                }else {
                    throw new ServiceException(ResultCode.DATA_CAN_NOT_DELETE);
                }
            }
        });
        // 判断validIds是否有值，如果没有值，抛出异常
        if(CollUtil.isEmpty(validIds)){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }else {
            managerFenceHouseMapper.deleteBatch(validIds);
        }
    }




}
